package com.ruoyi.mp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mp.domain.MpArticle;
import com.ruoyi.mp.domain.bo.MpArticleBo;
import com.ruoyi.mp.domain.vo.MpArticleVo;
import com.ruoyi.mp.mapper.MpArticleMapper;
import com.ruoyi.mp.service.IMpArticleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 公众号文章Service业务层处理
 *
 * @author yh
 * @date 2025-05-05
 */
@RequiredArgsConstructor
@Service
public class MpArticleServiceImpl implements IMpArticleService {

    private final MpArticleMapper baseMapper;

    /**
     * 查询公众号文章
     */
    @Override
    public MpArticleVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    @Override
    public MpArticleVo queryFarthestUncreatedArticle() {
        MpArticleVo farthestUncreatedArticle = baseMapper.selectVoOne(new LambdaQueryWrapper<MpArticle>()
            .eq(MpArticle::getStatus, 0)
            .orderByDesc(MpArticle::getCreateTime)
            .last("limit 1"));
        return farthestUncreatedArticle;
    }

    /**
     * 查询公众号文章列表
     */
    @Override
    public TableDataInfo<MpArticleVo> queryPageList(MpArticleBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<MpArticle> lqw = buildQueryWrapper(bo);
        Page<MpArticleVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询公众号文章列表
     */
    @Override
    public List<MpArticleVo> queryList(MpArticleBo bo) {
        LambdaQueryWrapper<MpArticle> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<MpArticle> buildQueryWrapper(MpArticleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MpArticle> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTheme()), MpArticle::getTheme, bo.getTheme());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), MpArticle::getType, bo.getType());
        lqw.like(StringUtils.isNotBlank(bo.getTitle()), MpArticle::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getContentHtml()), MpArticle::getContentHtml, bo.getContentHtml());
        lqw.eq(bo.getStatus() != null, MpArticle::getStatus, bo.getStatus());
        lqw.between(params.get("beginPublishTime") != null && params.get("endPublishTime") != null,
            MpArticle::getPublishTime, params.get("beginPublishTime"), params.get("endPublishTime"));
        return lqw;
    }

    /**
     * 新增公众号文章
     */
    @Override
    public Boolean insertByBo(MpArticleBo bo) {
        MpArticle add = BeanUtil.toBean(bo, MpArticle.class);
        if (StringUtils.isEmpty(add.getDelFlag())) {
            add.setDelFlag(UserConstants.DEL_FLAG_FALSE);
        }
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改公众号文章
     */
    @Override
    public Boolean updateByBo(MpArticleBo bo) {
        MpArticle update = BeanUtil.toBean(bo, MpArticle.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(MpArticle entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除公众号文章
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
